package plotter2d;

import javax.swing.*;
import java.awt.*;
import java.util.List;

/**
 * Created by IntelliJ IDEA.
 * User: Bezdelnick
 * Date: 03.05.13
 * Time: 11:50
 * To change this template use File | Settings | File Templates.
 */
public class GraphicsCanvas extends JPanel {
    private Point centerPoint = new Point(300, 300);
    private int graphScaleX = 20;
    private int graphScaleY = 20;

    public Point getCenterPoint() {
        return centerPoint;
    }

    public void setCenterPoint(Point centerPoint) {
        this.centerPoint = centerPoint;
    }

    public int getGraphScaleX() {
        return graphScaleX;
    }

    public void setGraphScaleX(int graphScaleX) {
        this.graphScaleX = graphScaleX;
    }

    public int getGraphScaleY() {
        return graphScaleY;
    }

    public void setGraphScaleY(int graphScaleY) {
        this.graphScaleY = graphScaleY;
    }
    
    public void clearCanvas() {
        Graphics graphics = this.getGraphics();
        graphics.clearRect(0, 0, 1000, 1000);
        graphics.setColor(new Color(239, 239, 239));
        //graphics.drawRect(0, 0, getWidth(), getHeight());
        graphics.fillRect(0, 0, getWidth(), getHeight());
        setBackground(new java.awt.Color(239, 239, 239));
    }
    
    public void resetCenterPoint() {
        setCenterPoint(new Point(getWidth()/2, getHeight()/4*3));
    }

    public void drawAxis() {
        Graphics graphics = this.getGraphics();
        graphics.setColor(Color.black);

        graphics.drawLine(10, centerPoint.y, getWidth() - 10, centerPoint.y);
        graphics.drawLine(centerPoint.x, 10, centerPoint.x, getHeight() - 10);
    }

    public void drawGraph(List<FloatPoint> pointList, Color color) {
        Graphics2D graphics = (Graphics2D) this.getGraphics();
        graphics.setColor(color);
        graphics.setStroke(new BasicStroke(3));
        
        for (int i = 0; i < pointList.size() - 1; i++) {
            graphics.drawLine(
                    (int)(centerPoint.x + getGraphScaleX() * pointList.get(i).getX()),
                    (int)(centerPoint.y + getGraphScaleY() * pointList.get(i).getY()),
                    (int)(centerPoint.x + getGraphScaleX() * pointList.get(i + 1).getX()),
                    (int)(centerPoint.y + getGraphScaleY() * pointList.get(i + 1).getY()));
        }
    }
}
